
if (typeof Widget == "undefined") 
    Widget = {};

if (typeof Widget.Select == "undefined") 
    Widget.Select = {};

Widget.Select.VERSION = '0.01';


Widget.Select.selectAll = function(source){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    var l = source.options.length;
    for (var j = 0; j < l; j++) {
        source.options[j].selected = true;
    }
}


Widget.Select.selectNone = function(source){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    var l = source.options.length;
    for (var j = 0; j < l; j++) {
        source.options[j].selected = false;
    }
}


Widget.Select.invertSelection = function(source){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    var l = source.options.length;
    for (var j = 0; j < l; j++) {
        source.options[j].selected = !source.options[j].selected;
    }
}

Widget.Select.addOption = function(target, text, value){
    if (typeof(target) != 'object') {
        target = document.getElementById(target);
	}
    var opt = new Option(text, value);
    target.options[target.options.length] = opt;
}

Widget.Select._moveOption = function(e, source, s_idx, target){
    var opt = new Option(e.text, e.value);
    opt.selected = e.selected;
    target.options[target.options.length] = opt;
    source.options[s_idx] = null;
}


Widget.Select.moveSelectedOptionsUp = function(source){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    var l = source.options.length;
    for (var j = 0; j < l; j++) {
    
        var e = source.options[0];
        if (e.selected) {
            Widget.Select._moveOption(e, source, 0, source, l);
            continue;
        }
        
        
        while (j < l - 1) {
            var f = source.options[1];
            if (!f.selected) 
                break;
            Widget.Select._moveOption(f, source, 1, source, l);
            j++;
        }
        
        Widget.Select._moveOption(e, source, 0, source, l);
    }
    
}


Widget.Select.moveSelectedOptionsDown = function(source){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    var l = source.options.length;
    var skip = 0;
    for (var j = 0; j < l; j++) {
        var e = source.options[0];
        if (skip == 0) {
            if (e.selected) {
                for (var i = 1; i < l - j; i++) {
                    var f = source.options[i];
                    if (!f.selected) {
                        Widget.Select._moveOption(f, source, i, source, l);
                        j++;
                        break;
                    }
                    skip++;
                    
                }
                
            }
        }
        else {
            skip--;
        }
        
        Widget.Select._moveOption(e, source, 0, source, l);
        
    }
    
}



Widget.Select.moveSelectedOptionsTo = function(source, target){
    if (typeof(source) != 'object') 
        source = document.getElementById(source);
    if (typeof(target) != 'object') 
        target = document.getElementById(target);
    for (var i = 0; i < source.options.length; i++) {
        var e = source.options[i];
        if (e.selected) {
            Widget.Select._moveOption(e, source, i, target, target.options.length);
            i--;
        }
    }
}

/*

 */
function llenarTabla(respJSON) {
	var objDDL = dojo.byId(respJSON.nombreTabla);
	var cabecera = objDDL.createTHead();
	var cuerpo = objDDL.tBodies[0];
	
	cabecera.innerHTML = "";
	cuerpo.innerHTML = "";
	
	var titulos = new Array();
	if (respJSON.items.length > 0) {
		var fila	=	respJSON.items[0];
		try{
		   var filaCabecera = cabecera.insertRow();
		}catch (e){
		   var filaCabecera = cabecera.insertRow(-1);
		}
		var contenidoCabecera = "";
		for (var i in fila) {
			titulos.push(i);
			var celdaTitulo = "<th scope='col'>" + i + "</th>";
			contenidoCabecera += celdaTitulo;
		}
		filaCabecera.innerHTML = contenidoCabecera;
	}
	
	for(i=1;i<respJSON.items.length;i++){
		var filaData	=	respJSON.items[i];
		try{
		   var filaTabla = cuerpo.insertRow();
		}catch (e){
		   var filaTabla = cuerpo.insertRow(-1);
		}
		
		for (j=0; j < titulos.length; j++) {
			try{
			   var celda = filaTabla.insertCell();
			}catch (e){
			   var celda = filaTabla.insertCell(-1);
			}
			var titulo	= titulos[j];
			var texto	= filaData[titulo];
			celda.innerHTML = texto;
		}
	}
}
